<?php
namespace Model;

use Illuminate\Database\Capsule\Manager as DB;
use Library\Log;


/**
 * Test Model
 * @author los_gsy
 */
class Test extends Base {
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
	}


	/**
	 * test
	 */
	static public function test() {
		return __METHOD__;
	}


	/**
	 * tx
	 */
	static public function tx() {
		//将配置项插入数据库
		$db = new Db;
		$db->startTransaction();
		$now = time();
		$cnf_data = [];
		$cnf_data['type'] = 'type' . $now;
		$cnf_data['k'] = 'k' . $now;
		$cnf_data['v'] = 'v';
		$cnf_data['idx'] = 1;
		$cnf_data['remark'] = 'remark';
		$cnf_data['enable'] = 1;
		$cnf_data['create_at'] = $now;
		$cnf_data['update_at'] = $now;
		$cnfId = $db->insert('cnf', $cnf_data);
		Log::debug(__METHOD__ . ': ' . $cnfId);

		//$db = new Db;
		$dictData = [];
		$dictData['code'] = 'code' . $now;
		$dictData['name'] = 'name';
		$dictData['type'] = 'type';
		$dictData['remark'] = 'remark';
		$dictData['create_at'] = $now;
		$dictData['update_at'] = $now;
		$dictId = $db->insert('dict', $dictData);
		Log::debug(__METHOD__ . ': ' . $dictId);
		//$db->commit();
		$db->rollback();

		exit('Success');
	}


	/**
	 * 获取总记录数
	 */
	static public function getCnt() {
		$result = DB::table(self::getTableName())
			->select(DB::raw('count(*) as cnt'))
			->first();
		if (!$result) {
			Log::error(__METHOD__ . ': $result = ' . var_export($result, true));
		}
		return $result;
	}


}